package httydgame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;

public class StartingClass extends Applet implements Runnable, KeyListener {
	
	private static final long serialVersionUID = 1L;
	private Dragon dragon;
	Font myFont;
	Color red;
	private static Background background1, background2;
	private ArrayList <WhisperingDeath> whisperingDeaths = new ArrayList<WhisperingDeath>();
	private Image image, character, water, whisperingDeath, fireball, background;
	private int points;
	private URL base;
	private Graphics second;
	private long space = 0, time = 0;
	Random random = new Random();
	@Override
	public void init() {
		setSize(800, 480);
		setBackground(Color.WHITE);
		red = Color.red;
		setFocusable(true);
		addKeyListener(this);
		Frame frame = (Frame) this.getParent().getParent();
		frame.setTitle("Dragon");
		try {
			base = getDocumentBase();
		} catch (Exception e) {
			// TODO: handle exception
		}
		character = getImage(base, "data/smallToothless.png");
		water = getImage(base, "data/water.png");
		whisperingDeath = getImage(base, "data/whisperingDeath.png");
		fireball = getImage(base, "data/fireball.png");
		background = getImage(base, "data/backgroundImage.jpg");
		try {
			myFont = Font.createFont(Font.TRUETYPE_FONT, new File("data/vineritc.ttf")).deriveFont(24f);
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            //register the font
            ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("vineritc.ttf")));
		} catch (FontFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}  
	 
	@Override
	public void start() {
		dragon = new Dragon();
		background1 = new Background(0, 300);
		background2 = new Background(-1200, 300);
		setFont(myFont);
		Thread thread = new Thread(this);
		thread.start();
		
	}
	
	@Override
	public void stop() {
	}
	
	@Override
	public void destroy() {
	}
	
	
	
	@Override
	public void run() {
		
		while (dragon.getLives() > 0)
		{
			points += 5; 
			int x = random.nextInt(100) + 1;
			if (x > 99)
			{
				whisperingDeaths.add(new WhisperingDeath(900, random.nextInt(300) + 50, -4));
			}
			
			dragon.update();
			
			ArrayList projectiles = dragon.getProjectiles();
			for (int n = 0; n < projectiles.size(); n++)
			{
				Projectile p = (Projectile)projectiles.get(n);
				if (p.isVisible())
				{
					p.update();
				}
				else
				{
					projectiles.remove(n);
				}
			}
			for (int n = 0; n < whisperingDeaths.size(); n++)
			{
				WhisperingDeath w = (WhisperingDeath)whisperingDeaths.get(n);
				if (w.isVisible())
				{
					w.update();
				}
				else
				{
					if (w.isDestroyed())
					{
						points += 3000;
					}
					whisperingDeaths.remove(n);
				}
			}
			
			collisionDetection();
			background1.update();
			background2.update();
			repaint();
			try{
				Thread.sleep(17);
			} catch (InterruptedException e){
				e.printStackTrace();
			}
		}
		if (dragon.getLives() == 0)
		{
			Graphics g = getGraphics();
			g.drawString("GAME OVER", 350, 200);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()){
		case KeyEvent.VK_UP:
			dragon.moveUp();
//			System.out.println("UP");
			break;
		case KeyEvent.VK_DOWN:
			dragon.moveDown();
//			System.out.println("DOWN");
			break;
		case KeyEvent.VK_LEFT:
			dragon.moveLeft();
//			System.out.println("LEFT");
			break;
		case KeyEvent.VK_RIGHT:
			dragon.moveRight();
//			System.out.println("RIGHT");
			break;
		case KeyEvent.VK_SPACE:
			if (System.currentTimeMillis() - space > 100)
			{
				dragon.shoot();
				space = System.currentTimeMillis();
			}
			break;
		}
		
	}
	
	public void loseLife(){
		time = System.currentTimeMillis();
		dragon.setLives(dragon.getLives() - 1);
	}

	@Override
	public void keyReleased(KeyEvent e) {
		switch(e.getKeyCode()){
		case KeyEvent.VK_UP:
			dragon.stopY();
			break;
		case KeyEvent.VK_DOWN:
			dragon.stopY();
			break;
		case KeyEvent.VK_LEFT:
			dragon.stopX();
			break;
		case KeyEvent.VK_RIGHT:
			dragon.stopX();
			break;
		case KeyEvent.VK_SPACE:
			break;
		}
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void update(Graphics g)
	{
		if (image == null) {
			image = createImage(this.getWidth(), this.getHeight());
			second = image.getGraphics();
		}


		second.setColor(getBackground());
		second.fillRect(0, 0, getWidth(), getHeight());
		second.setColor(getForeground());
		paint(second);


		g.drawImage(image, 0, 0, this);

	}
	
	public void collisionDetection()
	{
		ArrayList <Projectile> projectiles = dragon.getProjectiles();
		for (WhisperingDeath w: whisperingDeaths)
		{	if (w.isVisible())
			{
				if (dragon.rect1.intersects(w.rect1)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect2.intersects(w.rect2)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect1.intersects(w.rect3)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect2.intersects(w.rect1)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect1.intersects(w.rect2)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect2.intersects(w.rect3)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect3.intersects(w.rect1)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect3.intersects(w.rect2)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
				if (dragon.rect3.intersects(w.rect3)){
					System.out.println("Collision!");
					loseLife();
					w.setVisible(false);
				}
			}
			for (Projectile p: projectiles)
			{
				if (p.isVisible())
				{
					if (p.rect.intersects(w.rect1)){
						System.out.println("Hit!");
						w.setHealth(w.getHealth() - 1);
						p.setVisible(false); 
						points += 500;
					}
					if (p.rect.intersects(w.rect2)){
						System.out.println("Hit!");
						w.setHealth(w.getHealth() - 1);
						p.setVisible(false);
						points += 500;
					}
					if (p.rect.intersects(w.rect3)){
						System.out.println("Hit!");
						w.setHealth(w.getHealth() - 1);
						p.setVisible(false);
						points += 500;
					}
				}
			}
			
		}
	}
	
	@Override
	public void paint(Graphics g)
	{
		if (System.currentTimeMillis() > time + 50)
			{
			g.drawImage(background, 0, 0, this);
			g.drawImage(water, background1.getBgX(), background1.getBgY() + 75, this);
			g.drawImage(water, background2.getBgX(), background2.getBgY() + 75, this);
			for (WhisperingDeath w: whisperingDeaths)
			{
				if (w.isVisible())
				{
	//				g.drawRect((int)w.rect1.getX(), (int)w.rect1.getY(), (int)w.rect1.getWidth(), (int)w.rect1.getHeight());
	//				g.drawRect((int)w.rect2.getX(), (int)w.rect2.getY(), (int)w.rect2.getWidth(), (int)w.rect2.getHeight());
	//				g.drawRect((int)w.rect3.getX(), (int)w.rect3.getY(), (int)w.rect3.getWidth(), (int)w.rect3.getHeight());
					g.drawImage(whisperingDeath, w.getCenterX() - 75, w.getCenterY() - 45, this);
				}
				else 
				{
					break;
				}
			}
	//		g.drawRect((int)dragon.rect1.getX(), (int)dragon.rect1.getY(), (int)dragon.rect1.getWidth(), (int)dragon.rect1.getHeight());
	//		g.drawRect((int)dragon.rect2.getX(), (int)dragon.rect2.getY(), (int)dragon.rect2.getWidth(), (int)dragon.rect2.getHeight());
	//		g.drawRect((int)dragon.rect3.getX(), (int)dragon.rect3.getY(), (int)dragon.rect3.getWidth(), (int)dragon.rect3.getHeight());
			g.drawImage(character, dragon.getCenterX()-100, dragon.getCenterY()- 39, this);
			ArrayList projectiles = dragon.getProjectiles();
			for (int n = 0; n < projectiles.size(); n++)
			{
				Projectile p = (Projectile) projectiles.get(n);
				if (p.isVisible())
				{
					g.drawImage(fireball, p.getX() + 25, p.getY() + 25, this);
				}
				else
				{
					break;
				}
			}
		}
		else
		{
			g.setColor(Color.RED);
			g.fillRect(0, 0, 800, 480);
		}
		g.drawString("Lives: " + dragon.getLives(), 50, 50);
		g.drawString("Points: " + points, 50, 100);
		
		
	}
	
	 public static Background getBackground1() {
	        return background1;
	    }

	    public static Background getBackground2() {
	        return background2;
	    }
}
	
	
	

